package com.ljx.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class AddJFrame extends JFrame implements ActionListener {

    int count;

    //标题输入框
    JTextField titleText = new JTextField();

    //内容的输入区域
    JTextArea contentText = new JTextArea();

    //保存按钮
    JButton save = new JButton("保存");

    //取消按钮
    JButton cancel = new JButton("取消");
    public AddJFrame() throws IOException
    {
        //初始化次数数据
        BufferedReader cbr = new BufferedReader(new FileReader("src/count.txt"));
        String countStr = cbr.readLine();
        cbr.close();
        count = Integer.parseInt(countStr);

        initJFrame();

        initView();

        this.setVisible(true);

    }

    private void initView() {
        //定义最上面的每日一记
        JLabel title = new JLabel("每日一记");
        title.setBounds(240, 20, 584, 50);
        title.setFont(new Font("微软雅黑", Font.BOLD, 30));
        this.getContentPane().add(title);

        //定义文字：标题
        JLabel subject = new JLabel("标题");
        subject.setBounds(70,90,100,30);
        subject.setFont(new Font("微软雅黑",Font.PLAIN,16));
        this.getContentPane().add(subject);

        //定义文字：内容
        JLabel content = new JLabel("内容");
        content.setBounds(70,140,100,30);
        content.setFont(new Font("微软雅黑",Font.PLAIN,16));
        this.getContentPane().add(content);


        //设置标题的输入框
        titleText.setBounds(120,90,426,30);
        titleText.setFont(new Font("微软雅黑",Font.PLAIN,15));
        this.getContentPane().add(titleText);

        //设置内容的输入框
        contentText.setBounds(120,140,426,330);
        contentText.setFont(new Font("微软雅黑",Font.PLAIN,15));
        this.getContentPane().add(contentText);

        //设置保存按钮
        save.setBounds(132,500,140,40);
        save.setFont(new Font("微软雅黑",Font.PLAIN,20));
        save.addActionListener(this);
        this.getContentPane().add(save);

        //设置取消按钮
        cancel.setBounds(312,500,140,40);
        cancel.setFont(new Font("微软雅黑",Font.PLAIN,20));
        cancel.addActionListener(this);
        this.getContentPane().add(cancel);
    }

    private void initJFrame() {
        //设置界面的宽高
        this.setSize(600, 600);
        //设置界面的标题
        this.setTitle("每日一记（添加日记）");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认的居中放置，只有取消了才会按照XY轴的形式添加组件
        this.setLayout(null);
        //设置背景颜色
        this.getContentPane().setBackground(new Color(212,212,212));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == save)
        {
            System.out.println("保存");
            //保存逻辑
            //就是创建一个文件，按照序号count来起名创建，方便之后的打开操作
            //创建文件输出流
            try {
                //创建文件+输入内容
                BufferedWriter bw = new BufferedWriter(new FileWriter("src/files/"+count+".txt"));
                String title = titleText.getText();
                String content = contentText.getText();
                bw.write(title);
                bw.newLine();
                bw.write(content);
                bw.newLine();
                bw.close();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            //统计数要更新
            count++;
            //次数也要保存到文件中
            try {
                BufferedWriter cbw = new BufferedWriter(new FileWriter("src/count.txt"));
                cbw.write(count+"");
                cbw.newLine();
                cbw.close();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            //保存完成，回到主界面
            //先要关闭当前界面
            this.setVisible(false);
            try {
                new MainJFrame();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
        else if(e.getSource() == cancel)
        {
            System.out.println("取消");
            //取消逻辑：
            //把内容全部擦除，然后回到主界面
            titleText.setText("");
            contentText.setText("");
            try {
                new MainJFrame();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
